package org.jeecg.modules.wlhy.bill.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.bill.entity.HyCaptainBill;
import org.jeecg.modules.wlhy.bill.entity.HyCarrierBill;
import org.jeecg.modules.wlhy.bill.service.IHyCaptainBillService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.DataMaskingUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags="hy_captain_bill")
@RestController
@RequestMapping("/bill/hyCaptainBill")
@Slf4j
public class HyCaptainBillController {
    @Resource
    private IHyCaptainBillService hyCaptainBillService;
    @Resource
    private IHyShippingNoteService hyShippingNoteService;
    @Resource
    private IHySettingService settingService;
    @ApiOperation(value="hy_captain_bill-分页列表查询", notes="hy_captain_bill-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HyCaptainBill hyCaptainBill,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        String shippingNoteNumber = req.getParameter("shippingNoteNumber");
        QueryWrapper<HyCaptainBill> queryWrapper = QueryGenerator.initQueryWrapper(hyCaptainBill, req.getParameterMap());
        if(StringUtils.isNotEmpty(shippingNoteNumber)){
            try {
                HyShippingNote shippingNote = hyShippingNoteService.queryByShippingNoteNumber(shippingNoteNumber);
                if (shippingNote != null) {
                    queryWrapper.eq("shipping_id", shippingNote.getId());
                } else {
                    queryWrapper.eq("shipping_id", "-1");
                }
            }catch(Exception e){
                queryWrapper.eq("shipping_id", "-1");
            }
        }
        Page<HyCaptainBill> page = new Page<HyCaptainBill>(pageNo, pageSize);
        IPage<HyCaptainBill> pageList = hyCaptainBillService.page(page, queryWrapper);

        //如果开启系统脱敏
        String dataMaskingFlag = this.settingService.getSettingValueByTag("data_masking");
        if("1".equals(dataMaskingFlag)) {
            for (HyCaptainBill dto : pageList.getRecords()) {
                dto.setBillNum(DataMaskingUtil.bankNumberMasking(dto.getBillNum()));
                dto.setCraccno(DataMaskingUtil.bankNumberMasking(dto.getCraccno()));
                dto.setDbaccno(DataMaskingUtil.bankNumberMasking(dto.getDbaccno()));
            }
        }

        return Result.OK(pageList);
    }
}
